package cn.zwx.learn.basic.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.zwx.learn.basic.generate.SnowflakeIdWorker;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @description:
 * @projectName:java-basic
 * @see:cn.zwx.learn.basic.controller
 * @author:zhangwenxue
 * @createTime:2021/3/16 16:44
 * @version:1.0
 */
@RestController
public class TestController {

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    private RedisTemplate redisTemplate;


    @RequestMapping("/test")
    public String testId(){
        Long aLong = snowflakeIdWorker.generateId();
        redisTemplate.opsForSet().add("testKey",aLong);

        Object testCount = redisTemplate.opsForValue().get("testCount");
        if (Objects.nonNull(testCount)){
            redisTemplate.opsForValue().increment("testCount");
        }else {
            redisTemplate.opsForValue().setIfAbsent("testCount",1);
        }
        return JSON.toJSONString(aLong);
    }
}
